package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: cyqaaq
 * @since: 2022/3/15 14:06
 */
@RestController
@RequestMapping("admin/product")
public class BaseManageController {
    // 控制器：注入服务层
    @Autowired
    private ManageService manageService;

    @GetMapping("getCategory1")
    public Result getCategory1() {
        // 返回数据
        return Result.ok(manageService.getBaseCategory1());
    }

    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id) {
        return Result.ok(manageService.getBaseCategory2(category1Id));
    }

    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id) {
        return Result.ok(manageService.getBaseCategory3(category2Id));
    }

    // 根据分类id获取平台属性
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable("category1Id") Long category1Id,
                               @PathVariable("category2Id") Long category2Id,
                               @PathVariable("category3Id") Long category3Id) {
        return Result.ok(manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id));
    }

    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    // 根据平台属性id 获取平台属性对象数据
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        // 先判断一下当前是否存在该属性：如果有属性，则调用属性对象的属性值集合
        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        if (baseAttrInfo != null) {
            return Result.ok(baseAttrInfo.getAttrValueList());
        }
        return Result.ok();
//        return Result.ok(manageService.getAttrValueList(attrId));
    }
}
